const Actions = require('./actions');
const DB = require('app/db');
import { Dialog,Toast } from 'saltui';
import {Storage} from 'clientConfig/util/StoreData'
import { Control } from 'react-keeper';
import pinyin from 'simple-pinyin';
const getPhonetic = (value) => {
    return pinyin(value.name, { pinyinOnly: false });
}
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },

    onFetch: function (params, cb) {
        let t = this;
        let status = params.title != '允许出校' ? 'off' : 'on';
        DB.Teacher.get_class_inresidence({
            schoolId: params.schoolId || Storage.get('user.schoolId'),
            classId: params.classId,
            status: status
        })
            .then((content) => {
                if (content.code == 1) {
                    let classId = content.check.classId
                    content = content.data.list;
                    if (content && content.length > 0) {
                        var obj = {};
                        var select_str = ''
                        for (let item of content) {
                            item.phonetic = getPhonetic(item);
                            if (!obj.hasOwnProperty(item.phonetic[0][0])) {
                                let data_arr = [];
                                data_arr.push(item);
                                obj[item.phonetic[0][0]] = data_arr;
                                select_str += item.phonetic[0][0];
                            } else {
                                let data_arr = obj[item.phonetic[0][0]];
                                data_arr.push(item);
                                obj[item.phonetic[0][0]] = data_arr;
                            }
                        }
                        //存储数据
                        t.data.data_studentList = {
                            [classId]: obj
                        }
                        t.data.select_str = select_str;
                        t.data.listdata = obj;
                        t.updateComponent();
                    } else {
                        Dialog.alert({
                            title: `当前没有可${params.title}的学生`,
                            content: '点击确定返回',
                            onConfirm() {
                                Control.go(-1)
                            },
                        });
                    }
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onIspermission: function (params, cb) {
        let t = this;
        let arr = [];
        let obj = {};
        for (let item of params.selectId) {
            obj.id = item;
            arr.push(obj);
            obj = {};
        }
        let data = { studentList: arr }
        DB.Teacher.permission_student_isgoout({
            schoolId: params.schoolId || Storage.get('user.schoolId'),
            classId: params.classId,
            status: params.status,
            data: JSON.stringify(data)
        })
            .then((content) => {
                if (content.code == 1) {
                    Control.go(-1)
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
